//
//  OIDCacheManager.m
//  EducationOID
//
//  Created by slz on 2017/6/26.
//  Copyright © 2017年 bnsr. All rights reserved.
//

#import "OIDCacheManager.h"

#define Oid_History_key_Creat @"Oid_History_key_Creat_1"
#define Oid_History_key_analysis @"Oid_History_key_analysis_1"

@implementation OIDCacheManager

+ (OIDCacheManager *)shareInstance{
    static dispatch_once_t once;
    static OIDCacheManager *manager = nil;
    dispatch_once(&once, ^{
        manager = [[self alloc]init];
    });
    manager.analysisKey = Oid_History_key_analysis;
    manager.creatKey = Oid_History_key_Creat;
    return manager;
}

- (void)updateWithCreatOidString:(NSString *)oidUrl{
    if ([MFWStringUtils isEmptyString:oidUrl]) return;
    NSTimeInterval time = [[NSDate date] timeIntervalSince1970];
    NSArray * historyUrlArray = [CacheDB valueArrayForKey:self.creatKey];
    NSMutableArray * urlArray = nil;
    if (!historyUrlArray || historyUrlArray.count == 0){
        urlArray = @[].mutableCopy;
    }else{
        urlArray = historyUrlArray.mutableCopy;
    }
    NSDictionary * dict = @{@"url":oidUrl,@"time":@(time)};
    [urlArray insertObject:dict atIndex:0];
    [CacheDB setValueArray:urlArray forKey:self.creatKey];
//    if ([urlArray containsObject:oidUrl]){
//        [urlArray insertObject:oidUrl atIndex:0];
//        [CacheDB setValueArray:urlArray forKey:self.creatKey];
//    }
}

-(void)uopdateWithAnalysisOidString:(NSString *)oidUrl{
    if ([MFWStringUtils isEmptyString:oidUrl]) return;
    NSTimeInterval time = [[NSDate date] timeIntervalSince1970];
    NSArray * historyUrlArray = [CacheDB valueArrayForKey:self.analysisKey];
    NSMutableArray * urlArray = nil;
    if (!historyUrlArray || historyUrlArray.count == 0){
        urlArray = @[].mutableCopy;
    }else{
        urlArray = historyUrlArray.mutableCopy;
    }
    NSDictionary * dict = @{@"url":oidUrl,@"time":@(time)};
    [urlArray insertObject:dict atIndex:0];
    [CacheDB setValueArray:urlArray forKey:self.analysisKey];
//    if (![urlArray containsObject:oidUrl]){
//        [urlArray insertObject:oidUrl atIndex:0];
//        [CacheDB setValueArray:urlArray forKey:self.analysisKey];
//    }
}

- (NSArray *)getCreatOidList{
    NSArray *arr = [CacheDB valueArrayForKey:self.creatKey];
    if(arr == nil){
        arr = @[].mutableCopy;
    }
    return arr;
}

- (NSArray *)getAnalysisOidList{
    NSArray *arr=[CacheDB valueArrayForKey:self.analysisKey];
    if (arr == nil)
    {
        arr = @[].mutableCopy;
    }
    return arr;
}


@end
